#!/bin/bash
# Build freon's package.list
# Version: 0.0.1
# (C) Chris Dorman, 2020 GPLv3+

HTDOCSPATH="/opt/htdocs/freon"
OUTPUTFILE="packages.html"
mirrorurl="https://mirror.freonlinux.com"

### HEADER HTML
HEADERHTML="<!DOCTYPE html>
<html>
<head>
    <title>Freon Linux ~ Package list</title>
    <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">
    <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">
</head>
<body>
<div id=\"navcontainer\">
    <div id=\"navbar\"><!--
    --><a href=\"index.html\">Home</a><!--
        --><a href=\"https://mirror.freonlinux.com\">Files</a><!--
        --><a href=\"https://notabug.org/Pentium44/FreonLinux\">Compile</a><!--
        --><a href=\"wiki.html\">Wiki</a><!--
        --><a href=\"packages.html\">Repo</a><!--
        --></div>
</div>
<div id=\"contentcontainer\">
    <div id=\"content\">
        <table class=\"sidebar\">
        <tr>
        <td class=\"contentbody\">
            <h2>Freon Linux ~ Packages</h2>
            <p>"

### FOOTER HTML
FOOTERHTML="</p>
            <h3>Package sources</h3>
            <p>All of our sources can be found <a href=\"$mirrorurl/source/packages\">here</a>
            or in our <a href=\"https://notabug.org/Pentium44/FreonLinux\">notabug.org repository</a></p>
        </tr>
        </table>
    </div>
    <div id=\"footer\">
        &copy; Freon Linux, 2018-2021 GPLv3 (Software provided in Freon is subject to their own license agreements)
    </div>
</div>

</body>
</html>"

echo "MAKE SURE THE ONLY .tgz FILES ARE PACKAGES IN THE REPO WITHIN THE SAME DIRECTORY OF THIS FILE!"

sleep 1

echo $HEADERHTML > $HTDOCSPATH/$OUTPUTFILE

echo "<ul>" >> $HTDOCSPATH/$OUTPUTFILE

for f in *.tgz
do
    filename=${f%.tgz}
    pkgdesc=$(cat ../pkginfo/$filename.txt)
	echo "<li><a href='$mirrorurl/packages/$filename.tgz'>$filename</a> $pkgdesc" >> $HTDOCSPATH/$OUTPUTFILE

    if [ -f ../deps/$filename.deps ]; then
        echo "<br /><span style='font-size:12px;'>dependencies:" >> $HTDOCSPATH/$OUTPUTFILE

        last_line=$(wc -l < ../deps/$filename.deps)
        current_line=0

        while IFS= read -r line
        do
            current_line=$(($current_line + 1))
            if [[ $current_line -ne $last_line ]]; then
                echo "<a href='$mirrorurl/packages/$line.tgz'>$line</a>, " >> $HTDOCSPATH/$OUTPUTFILE
            else
                echo "<a href='$mirrorurl/packages/$line.tgz'>$line</a>.</span></li>" >> $HTDOCSPATH/$OUTPUTFILE
            fi
        done < "../deps/$filename.deps"
    else
        echo "</li>" >> $HTDOCSPATH/$OUTPUTFILE
    fi

done

echo "</ul>" >> $HTDOCSPATH/$OUTPUTFILE

echo $FOOTERHTML >> $HTDOCSPATH/$OUTPUTFILE
